//****************************
//** CSpriteHigh
//**
//** High-Level sprite class
//**
//****************************

#include "stdafx.h"
#include "CSpriteLow.h"
#include "CSpriteHigh.h"

CSpriteHigh::CSpriteHigh(HDC hdc): CSpriteLow(hdc)
{
	
    m_iCurFrame = 0;
    m_iTotalFrames = 0;

	// *ACK* - move to LoadAnimSeq
    //for (int n = 0; n < NUM_FRAMES; n++)
    //    m_sprites[n] = new CImageHigh(hdc);
	m_hdc = hdc;

	m_bSaveBackground = TRUE;
	// set no "auto frame next"
	m_iFrameRate = 0; 
	m_iFrameCount = 0;

	m_iFirstFrame=-1;
	m_iLastFrame=-1;
	m_bLoopFrames=TRUE;

	m_bIsACopy = FALSE;

	m_bIsStreched = FALSE;
	m_iSaveWidth = 0; m_iSaveHeight = 0;
	
	m_iSpriteCollisionAccuracy = 0;

    m_bUseCollisionArray = FALSE;
 
}

CSpriteHigh::~CSpriteHigh()
{
	// *ACK* was NUM_FRAMES
	if (!GetIsACopy()) {
		for (int n = 0; n < m_iTotalFrames; n++)
			delete m_sprites[n];
	}
}


void CSpriteHigh::SetImageSize(int iImgWidth, int iImgHeight)
{
	// set the image size
    m_iWidth = iImgWidth;
    m_iHeight = iImgHeight;
}


int CSpriteHigh::LoadAnimSeq(int iStartX, int iStartY, int iNumFrames, LPTSTR lpFilename, int iWidth, int iHeight, int iFramesPerRow)
{
	// loads sprite's frames from an image file.
    CImageHigh *source;
    int iFrame;
	BOOL bDoBitBlt=TRUE;

    if (iNumFrames > NUM_FRAMES)
        return -1;

    if (iWidth < 1 || iWidth > 240 || iHeight < 1 || iHeight > 320)
        return -2;

    source = new CImageHigh(GetScreenDC());
    if (!source->Load(lpFilename))
    {
        bDoBitBlt=FALSE;
    }

	// first allocate frames
	for (iFrame = 0; iFrame < iNumFrames; iFrame++)
    {

   		m_sprites[iFrame] = new CImageHigh(m_hdc);
        if (!m_sprites[iFrame]->Create(iWidth, iHeight))
        {
            delete source;
            return -4;
        }
		m_sprites[iFrame]->m_iWidth = iWidth;
		m_sprites[iFrame]->m_iHeight = iHeight;

		m_iTotalFrames++;
	}
	
	// now load frame data
	if (bDoBitBlt) {
		for (iFrame = 0; iFrame < iNumFrames; iFrame++)
		{
			if (iFrame>0 && (iFrame % iFramesPerRow) == 0) {
				iStartY = iStartY + (iHeight);
				iStartX = 0;
			}

 
			BitBlt(m_sprites[iFrame]->GetSourceDC(),
				0, 0, iWidth, iHeight, 
				source->GetSourceDC(),
				iStartX, iStartY, SRCCOPY);
			iStartX = (iStartX + (iWidth));

			
		}
    }

    SetFrame(0);
    SetImageSize(iWidth, iHeight);
    delete source;

    return 0;
}

BOOL CSpriteHigh::BitBlit()
{
	// copy sprite to default DC
    return BitBlt(GetScreenDC(), (int) GetX(), (int) GetY(),
        ImageWidth(), ImageHeight(), 
        m_sprites[m_iCurFrame]->GetSourceDC(), 0, 0, SRCCOPY);
}

BOOL CSpriteHigh::StretchBlit(COLORREF clrTrans)
{
	// copy amd scale sprite to default DC
	BOOL bRetVal;
    HBITMAP hBmpTemp;
	HBITMAP hBmpOld;
    HDC hdcBmpTemp;

	// first make a temporary holding area
    hBmpTemp = CreateCompatibleBitmap(GetScreenDC(), ImageWidth(), ImageHeight());
    if (hBmpTemp == NULL)
        return FALSE;
    hdcBmpTemp = CreateCompatibleDC(GetScreenDC());
    if (hdcBmpTemp == NULL)
        return FALSE;
    hBmpOld = SelectBitmap(hdcBmpTemp, hBmpTemp);
    
	StretchBlt(hdcBmpTemp, 
        0, 0, ImageWidth(), ImageHeight(), 
        m_sprites[m_iCurFrame]->GetSourceDC(),
        0, 0, m_iSaveWidth, m_iSaveHeight, SRCCOPY);

	bRetVal = TransparentImage(GetScreenDC(), 
        (int) GetX(), (int) GetY(),
        ImageWidth(), ImageHeight(), 
        hdcBmpTemp,0, 0, 
        ImageWidth(), ImageHeight(), clrTrans);

	// cleanup
	SelectObject(hdcBmpTemp, hBmpOld);
	DeleteObject(hBmpTemp);
	DeleteDC(hdcBmpTemp);

	return bRetVal;
}

BOOL CSpriteHigh::TransBlit(COLORREF clrTrans)
{
	// copy sprite to default DC transparently
    return TransparentImage(GetScreenDC(), 
        (int) GetX(), (int) GetY(),
        ImageWidth(), ImageHeight(), 
        m_sprites[m_iCurFrame]->GetSourceDC(),0, 0, 
         ImageWidth(), ImageHeight(), clrTrans);
}

BOOL CSpriteHigh::TransBlit(HDC hdc, int x, int y, COLORREF clrTrans)
{
	// copy sprite to specific DC transparently
    return TransparentImage(hdc, 
        x, y,
        ImageWidth(), ImageHeight(), 
        m_sprites[m_iCurFrame]->GetSourceDC(),0, 0, 
        ImageWidth(), ImageHeight(), clrTrans);
}

void CSpriteHigh::SetFrame(int iFrame)
{
    if (iFrame >= 0 && iFrame < m_iTotalFrames)
        m_iCurFrame = iFrame; 
}

void CSpriteHigh::NextFrame()
{
	if (m_iFirstFrame > -1 && m_iLastFrame > -1) {
		// use frame range
		if (m_iLastFrame > m_iFirstFrame) {
			// cycle through frames forward
			if (m_iCurFrame < m_iLastFrame && m_iCurFrame >= m_iFirstFrame) {
				m_iCurFrame++;
			} else {
				if (m_bLoopFrames) { 
					m_iCurFrame = m_iFirstFrame;
				} else {
					m_iFrameRate = 0;
				}
			}
		} else {
			// cycle through frames backwards
			if (m_iCurFrame > m_iLastFrame) {
				m_iCurFrame--;
			} else {
				if (m_bLoopFrames) { 
					m_iCurFrame = m_iFirstFrame;
				} else {
					m_iFrameRate = 0;
				}
			}
		}
	} else {
		// use all frames
		if (m_iCurFrame < m_iTotalFrames - 1)
			m_iCurFrame++;
		else
			m_iCurFrame = 0;
	}
}

void CSpriteHigh::PrevFrame()
{
	if (m_iFirstFrame > -1 && m_iLastFrame > -1) {
	// use frame range
		if (m_iCurFrame > m_iFirstFrame && m_iCurFrame < m_iTotalFrames) {
			m_iCurFrame--;
		} else {
			if (m_bLoopFrames) { 
				m_iCurFrame = m_iLastFrame;
			} else {
				m_iFrameRate = 0;
			}
		}
	} else {
		// use all frames
		if (m_iCurFrame > 0)
			m_iCurFrame--;
		else
			m_iCurFrame = m_iTotalFrames - 1;
	}
}


void CSpriteHigh::SetSaveBackGround(BOOL bSetSave) {
	m_bSaveBackground = bSetSave;

}

BOOL CSpriteHigh::GetSaveBackGround() {
	return m_bSaveBackground;

}
